﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace PlayerTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            lbPlayers.ValueMember = "ID";
            lbPlayers.DisplayMember = "Name";
            RefreshPlayerList();
        }

        private void btnMakeMe_Click(object sender, EventArgs e)
        {
            using (SqlConnection conn = new SqlConnection("Data Source=JASGAMER\\SQLEXPRESS;Initial Catalog=test;User Id=dome;Password=bunnies;Trusted_Connection=True"))
            {
                try
                {
                    conn.Open();
                    SqlCommand getPlayers = new SqlCommand("spCreatePlayer", conn);
                    getPlayers.Parameters.AddWithValue("@Name", tbUser.Text);
                    getPlayers.Parameters.AddWithValue("@Password", tbPass.Text);
                    getPlayers.CommandType = CommandType.StoredProcedure;
                    int status = getPlayers.ExecuteNonQuery();
                    MessageBox.Show("Created Player");
                    conn.Close();
                }
                catch (Exception error)
                {
                    MessageBox.Show(error.Message);
                    if(conn.State == ConnectionState.Open)
                        conn.Close();                    
                }
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshPlayerList();
        }

        private void RefreshPlayerList()
        {
            if (lbPlayers.Items.Count > 0)
                lbPlayers.Items.Clear();
            using (SqlConnection conn = new SqlConnection("Data Source=JASGAMER\\SQLEXPRESS;Initial Catalog=test;User Id=dome;Password=bunnies;Trusted_Connection=True"))
            {
                try
                {
                    conn.Open();
                    SqlCommand getPlayers = new SqlCommand("spGetAllPlayers", conn);
                    getPlayers.CommandType = CommandType.StoredProcedure;
                    SqlDataReader sdr = getPlayers.ExecuteReader();
                    if (sdr.HasRows)
                    {
                        while (sdr.Read())
                        {
                            lbPlayers.Items.Add(
                                new Player(Convert.ToInt32(sdr["ID"].ToString()), sdr["Name"].ToString(), sdr["Password"].ToString()));
                        }
                    }
                    conn.Close();
                }
                catch (Exception error)
                {
                    MessageBox.Show(error.Message);
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        private void lbPlayers_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbCharacters.Items.Count > 0)
                lbCharacters.Items.Clear();
            using (SqlConnection conn = new SqlConnection("Data Source=JASGAMER\\SQLEXPRESS;Initial Catalog=test;User Id=dome;Password=bunnies;Trusted_Connection=True"))
            {
                try
                {
                    conn.Open();
                    SqlCommand getPlayers = new SqlCommand("spGetPlayersCharacters", conn);
                    getPlayers.Parameters.AddWithValue("@PlayerID", (lbPlayers.Items[lbPlayers.SelectedIndex] as Player).ID);
                    getPlayers.CommandType = CommandType.StoredProcedure;
                    SqlDataReader sdr = getPlayers.ExecuteReader();
                    if (sdr.HasRows)
                    {
                        while (sdr.Read())
                        {
                            lbCharacters.Items.Add(sdr["CharacterName"].ToString());
                        }
                    }
                    conn.Close();
                }
                catch (Exception error)
                {
                    MessageBox.Show(error.Message);
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
            RefreshSelectedPlayersCharacters();
        }

        private void tbCreateChar_Click(object sender, EventArgs e)
        {
            using (SqlConnection conn = new SqlConnection("Data Source=JASGAMER\\SQLEXPRESS;Initial Catalog=test;User Id=dome;Password=bunnies;Trusted_Connection=True"))
            {
                try
                {
                    conn.Open();
                    SqlCommand addPlayersCharacter = new SqlCommand("spAddPlayersCharacter", conn);
                    addPlayersCharacter.Parameters.AddWithValue("@PlayerID", (lbPlayers.Items[lbPlayers.SelectedIndex] as Player).ID);
                    addPlayersCharacter.Parameters.AddWithValue("@CharacterName", tbCharacterName.Text);
                    addPlayersCharacter.CommandType = CommandType.StoredProcedure;
                    SqlDataReader sdr = addPlayersCharacter.ExecuteReader();
                    if (sdr.HasRows)
                    {
                        while (sdr.Read())
                        {
                            lbCharacters.Items.Add(sdr["CharacterName"].ToString());
                        }
                    }
                    conn.Close();
                }
                catch (Exception error)
                {
                    MessageBox.Show(error.Message);
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        private void btnRefreshChars_Click(object sender, EventArgs e)
        {
            RefreshSelectedPlayersCharacters();
        }

        private void RefreshSelectedPlayersCharacters()
        {
            if (lbCharacters.Items.Count > 0)
                lbCharacters.Items.Clear();
            using (SqlConnection conn = new SqlConnection("Data Source=JASGAMER\\SQLEXPRESS;Initial Catalog=test;User Id=dome;Password=bunnies;Trusted_Connection=True"))
            {
                try
                {
                    conn.Open();
                    SqlCommand getPlayers = new SqlCommand("spGetPlayersCharacters", conn);
                    getPlayers.Parameters.AddWithValue("@PlayerID", (lbPlayers.Items[lbPlayers.SelectedIndex] as Player).ID);
                    getPlayers.CommandType = CommandType.StoredProcedure;
                    SqlDataReader sdr = getPlayers.ExecuteReader();
                    if (sdr.HasRows)
                    {
                        while (sdr.Read())
                        {
                            lbPlayers.Items.Add(sdr["CharacterName"].ToString());
                        }
                    }
                    conn.Close();
                }
                catch (Exception error)
                {
                    MessageBox.Show(error.Message);
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }
    }

    public class Player
    {
        public Player(int id, string name, string pw)
        {
            iID = id;
            sName = name;
            sPassword = pw;
        }
        public int ID
        {
            get { return iID; }
            set { iID = value; }
        }

        public string Name
        {
            get { return sName; }
            set { sName = value; }
        }

        public string Password
        {
            get { return sPassword; }
            set { sPassword = value; }
        }

        public List<string> Characters
        {
            get { return lsCharacters; }
            set { lsCharacters = value; }
        }

        private int iID;
        private string sName;
        private string sPassword;
        List<string> lsCharacters;
    }
}
